package com.cj.controller;

import com.cj.pojo.Comment;
import com.cj.pojo.PageBean;
import com.cj.pojo.Result;
import com.cj.pojo.dto.CommentQueryDTO;
import com.cj.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping
    public Result add(@RequestBody @Validated Comment comment) {
        commentService.add(comment);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody @Validated Comment comment) {
        commentService.update(comment);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        commentService.delete(id);
        return Result.success();
    }

    @PostMapping("/article/list")
    public Result<PageBean<Comment>> listByArticle(@RequestBody @Validated CommentQueryDTO queryDTO) {
        PageBean<Comment> pb = commentService.listByArticle(
            queryDTO.getId(),
            queryDTO.getPageNum(),
            queryDTO.getPageSize()
        );
        return Result.success(pb);
    }

    @PostMapping("/user/list")
    public Result<PageBean<Comment>> listByUser(@RequestBody @Validated CommentQueryDTO queryDTO) {
        PageBean<Comment> pb = commentService.listByUser(
            queryDTO.getId(),
            queryDTO.getPageNum(),
            queryDTO.getPageSize()
        );
        return Result.success(pb);
    }

    @PostMapping("/reply/{parentId}")
    public Result reply(@PathVariable Long parentId, @RequestBody @Validated Comment comment) {
        commentService.reply(parentId, comment);
        return Result.success();
    }

    @PatchMapping("/like/{id}")
    public Result like(@PathVariable Long id) {
        commentService.like(id);
        return Result.success();
    }

    @PostMapping("/replies/list")
    public Result<PageBean<Comment>> listReplies(@RequestBody @Validated CommentQueryDTO queryDTO) {
        PageBean<Comment> pb = commentService.listReplies(
            queryDTO.getId(),
            queryDTO.getPageNum(),
            queryDTO.getPageSize()
        );
        return Result.success(pb);
    }
}
